from etgui import *
from PageLayout import *
from AllModelDictEditors import PlatformModelDictEditor

def MakePagePlatform(model):
	platformDefault=PotGuiPopupButton("PlatformDefault")
	def UpdatePlatformDefaultPopup(model,path='',popup=platformDefault):
		popup.RemoveAllItems()
		itemsList=Cast(model).GetIndex("Platform")
		if itemsList:
			itemsList.sort()
			for v in itemsList:
				popup.AddItem(v,v)   # (display, model)
	model.AddModelObserver(UpdatePlatformDefaultPopup,"Platform")

	platformDictEditor=PlatformModelDictEditor(model,"Platform","Platform")
	def UpdateSelection(model,path,platformDictEditor=platformDictEditor):
		selection= Cast(model).GetValue(path)
		try:
			i=platformDictEditor.items.index(selection)
			platformDictEditor.Select(i)
		except (IndexError,ValueError):
			pass
	model.AddModelObserver(UpdateSelection,"PlatformDefault")
	
	return MakePageLayout(eVObjHExpand|eVObjVExpand,
		MakeSectionLayout(
			Form(
				items=[
					"Default Platform",MakeFieldLayout(platformDefault)
				]
			)
		),
		MakeModelDictEditorLayout(platformDictEditor)
	)
